using Finbuckle.MultiTenant;
using Microsoft.EntityFrameworkCore;
using MultiTenant.ShareDb.Api.Data;
using MultiTenant.ShareDb.Api.Extensions;


var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization();
builder.Services.AddAuthentication();
builder.Services.AddDbContext<TenantInfoStoreDbContext>(
    options => options.UseSqlite("Data Source=database.db"));
builder.Services.AddMultiTenant<TenantInfo>()
    .WithHeaderStrategy("X-Tenant")
    .WithEFCoreStore<TenantInfoStoreDbContext, TenantInfo>();

builder.Services.AddDbContext<ApplicationDbContext>(
    options => options.UseSqlite("Data Source=database.db"));

var app = builder.Build();
app.MapGet("/", (HttpContext httpContext) =>
{
    var tenantInfo = httpContext.GetMultiTenantContext<TenantInfo>().TenantInfo;
    if (tenantInfo is null)
    {
        return Results.BadRequest();
    }
    return Results.Ok(new
    {
        tenantInfo.Identifier,
        tenantInfo.Id
    });
});
app.MapGet("/api/products", async (ApplicationDbContext context) 
    => Results.Ok(await context.Products.ToListAsync()));
await app.ApplyMigrateAsync();
app.UseHttpsRedirection();
app.UseRouting();
app.UseMultiTenant();
app.UseAuthentication();
app.UseAuthorization();
app.Run();
